package com.koron.order.repair.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.repair.bean.entity.RepairItemPo;
import com.koron.order.repair.bean.vo.RepairItemVo;
import com.koron.order.repair.mapper.RepairItemMapper;
import com.koron.order.repair.service.RepairItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (RepairItem)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-23 15:53:22
 */
@Service("repairItemService")
public class RepairItemServiceImpl implements RepairItemService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, RepairItemPo repairItem) {

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);
        return mapper.insert(repairItem);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<RepairItemPo> repairItemPos) {

        if (CollectionUtils.isEmpty(repairItemPos)) {
            return 0;
        }

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);
        repairItemPos.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
            p.setCreateBy(getCurrUser().getAccount());
            p.setCreateByName(getCurrUser().getName());
        });
        //mapper.deleteByRepairEquipmentId(repairItemBeans.get(0).getRepairEquipId());
        mapper.deleteByRepairId(repairItemPos.get(0).getRepairId());
        return mapper.insertBatch(repairItemPos);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, RepairItemPo repairItem) {
        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);
        return mapper.update(repairItem);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public RepairItemPo queryById(SessionFactory factory, String id) {

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<RepairItemVo> queryByEquipmentId(SessionFactory factory, String orderId, String repairEquipmentId) {

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);

        List<RepairItemVo> itemVos = mapper.queryByEquipmentId(orderId, repairEquipmentId);

        if (CollectionUtils.isNotEmpty(itemVos)) {
            Map<String, RepairItemVo> map = new HashMap<>();
            for (RepairItemVo itemVo : itemVos) {
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                itemVo.setRepairItemVoList(new ArrayList<>());
                if (StringUtils.isEmpty(parentId)) {
                    map.put(id, itemVo);
                }
            }

            Iterator<RepairItemVo> iterator = itemVos.iterator();

            while (iterator.hasNext()) {
                RepairItemVo itemVo = iterator.next();
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                if (StringUtils.isNotEmpty(parentId)) {
                    RepairItemVo repairItemVo = map.get(parentId);
                    if (!Objects.isNull(repairItemVo)) {
                        //找到了父节点,应该从原来列表删除
                        iterator.remove();
                        List<RepairItemVo> repairItemVoList = repairItemVo.getRepairItemVoList();
                        repairItemVoList.add(itemVo);
                    }
                }
            }
        }

        return itemVos;
    }

    @Override
    @TaskAnnotation("queryList")
    public List<RepairItemPo> queryList(SessionFactory factory, RepairItemPo repairItem) {

        RepairItemMapper mapper = factory.getMapper(RepairItemMapper.class);

        List<RepairItemPo> list = mapper.queryList(repairItem);

        return list;
    }

}